//---------------------------------------------------------------------
//  This file is part of the Background Motion solution.
// 
//  Copyright (C) Mindscape (TM).  All rights reserved.
//  http://www.mindscape.co.nz
// 
//  THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//---------------------------------------------------------------------

using System;
using AjaxControlToolkit;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Mindscape.BackgroundMotion.Model;
using Mindscape.BackgroundMotion.Website.Controllers;
using Mindscape.BackgroundMotion.Website.Infrastructure;
using Mindscape.BackgroundMotion.Website.Views;

namespace Mindscape.BackgroundMotion.Website
{  
  public partial class View : PageBase<ViewPresenter, IViewView, IContributionController>, IViewView
  {
    private Contribution _contribution;
    private string _preview;

    /// <summary>
    /// Gets if the contribution being viewed has any comments
    /// </summary>
    protected bool HasComments
    {
      get
      {
        if (_contribution.Comments.Count > 0)
          return true;
        else
          return false;
      }
    }

    /// <summary>
    /// Handles the Changed event of the Rating control by adding the rating to the current contribution
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="AjaxControlToolkit.RatingEventArgs"/> instance containing the event data.</param>
    protected void Rating_Changed(object sender, RatingEventArgs e)
    {
      Presenter.AddRating((Contribution.RatingValue)Enum.Parse(typeof(Contribution.RatingValue), e.Value));
    }

    /// <summary>
    /// Handles the Changed event of the DownloadRating control by adding the rating to the current contribution
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="AjaxControlToolkit.RatingEventArgs"/> instance containing the event data.</param>
    protected void DownloadRating_Changed(object sender, RatingEventArgs e)
    {
      Presenter.AddDownloadExperienceRating((Contribution.RatingValue)Enum.Parse(typeof(Contribution.RatingValue), e.Value));
    }

    /// <summary>
    /// Handles the Click event of the AddCommentButton control by adding the comment to the current contribution
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void AddCommentButton_Click(object sender, EventArgs e)
    {
      Presenter.AddComment(SubjectTextBox.Text, MessageTextBox.Text);
    }

    /// <summary>
    /// Handles the Click event of the DeleteContribution control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void DeleteContribution_Click(object sender, EventArgs e)
    {
      Presenter.DeleteContribution(Contribution);
    }

    /// <summary>
    /// Handles the Click event of the ApproveContribution control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void ApproveContribution_Click(object sender, EventArgs e)
    {
      Presenter.ApproveContribution(Contribution);
    }

    /// <summary>
    /// The preview block to be rendered on the view
    /// </summary>
    public string Preview
    {
      get { return _preview; }
      set { _preview = value; }
    }

    /// <summary>
    /// The current rating of the download experience for this contribution
    /// </summary>
    public Contribution.RatingValue ContributionDownloadRating
    {
      set { DownloadRating.CurrentRating = (int) value; }
      get { return Contribution.DownloadExperienceRating; }
    }

    /// <summary>
    /// The current rating for the contribution
    /// </summary>
    public Contribution.RatingValue ContributionRating
    {
      set { Rating.CurrentRating = (int) value; }
      get { return Contribution.Rating; }
    }

    /// <summary>
    /// Indicates if we can add comments to this contribution
    /// </summary>
    public bool CanAddComments
    {
      set { CommentsUpdatePanel.Visible = value; }
    }

    /// <summary>
    /// Indicates if we can rate this contribution
    /// </summary>
    public bool CanRate
    {
      set
      {
        Rating.ReadOnly = !value;
        DownloadRating.ReadOnly = !value;
      }
    }

    /// <summary>
    /// Indicates if we can delete this contribution
    /// </summary>
    /// <value></value>
    public bool CanDelete
    {
      set 
      {
        AdminPanel.Visible = value;
      }
    }

    /// <summary>
    /// Indicates if we can approve this contribution
    /// </summary>
    public bool CanApprove
    {
      set
      {
        ApproveContribution.Visible = value;
      }
    }

    /// <summary>
    /// The contribution which has been requested
    /// </summary>
    public Contribution Contribution
    {
      get { return _contribution; }
      set
      {
        _contribution = value;

        if (_contribution != null)
        {
          DataBinder.DataBind();

          if (_contribution.ContentType.Description == "Image")
          {
            downloadLink.ImageUrl = "~/images/img-down.gif";
          }
        }
      }
    }

    /// <summary>
    /// A list of validation results to be shown in error if there are data entry errors on the contribution
    /// </summary>
    public ValidationResults ValidationResults
    {
      set
      {
        ErrorsRepeater.DataSource = value;
        ErrorsRepeater.DataBind();
      }
    }

    /// <summary>
    /// Removes the last comment from the contribution
    /// </summary>
    public void ClearLastComment()
    {
      SubjectTextBox.Text = MessageTextBox.Text = null;
    }

    /// <summary>
    /// Enables the rendering of Silverlight applications
    /// </summary>
    /// <remarks>
    /// The reason for adding the scripts via a placeholder control is that they need to be 
    /// injected after the ASP.NET AJAX scripts, otherwise they cause the AJAX scripts to fail.
    /// Note: ClientScript.RegisterClientScriptInclude causes them to be injected pre-AJAX.
    /// </remarks>
    public void EnableSilverlightScriptRendering(bool fromStreamingServer)
    {      
      if (fromStreamingServer)
      {
        StreamingSilverlightScripts.Visible = true;
      }
      else
      {
        SilverlightScripts.Visible = true;
      }
    }
  }
}